package com.qxzn.module.mine.activity;

import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.lmc.util.ImgCropUrtil;
import com.qxzn.app.api.api.HttpRequest;
import com.qxzn.app.bean.BaseBean;
import com.qxzn.app.bean.FaceDetectionBean;
import com.qxzn.app.page.BaseActivity;
import com.qxzn.app.page.robot.PermissionHelper;
import com.qxzn.app.page.util.GlideImageLoader;
import com.qxzn.app.page.widget.ToastUtil;
import com.qxzn.app.util.CommonUtil;
import com.qxzn.app.util.Constant;
import com.qxzn.module.mine.R;
import com.qxzn.module.mine.R2;
import com.zhihu.matisse.Matisse;
import com.zhihu.matisse.MimeType;
import com.zhihu.matisse.internal.entity.CaptureStrategy;

import java.io.File;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class UpdateFaceActivity extends BaseActivity {
    @BindView(R2.id.user_face)
    ImageView userFace;

    @Override
    protected int initLayout() {
        return R.layout.activity_update_face;
    }

    @Override
    protected void initView() {

    }

    @OnClick({R2.id.camera_btn, R2.id.picture_btn})
    public void onViewClicked(View view) {
        if (CommonUtil.isFastClick()) return;
        int i = view.getId();
        if (i == R.id.camera_btn) {
            PermissionHelper.checkCameraPermission(this, () ->
                    startActivity(new Intent()
                            .setComponent(new ComponentName(this, "com.qxzn.app.personalcenter.activity.RegisterFace2Activity"))
                            .putExtra("activity", "com.qxzn.module.mine.activity.UpdateFaceResultActivity")
                            .putExtra("jump", true)));
        } else if (i == R.id.picture_btn) {
            PermissionHelper.checkCameraPermission(this, () -> getPicture(1, 1));
        }
    }

    @Override
    protected void iniTitleBar() {
        tv_title.setText("重置人脸识别");
    }

    @Override
    protected void initData(Bundle savedInstanceState) {

    }

    private void getPicture(int maxSelectable, int requestCode) {
        Matisse.from(this)
                .choose(MimeType.ofImage())
                .countable(false)
                .capture(false)
                .maxSelectable(maxSelectable)
                .captureStrategy(new CaptureStrategy(true, Constant.authority))
                .restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
                .thumbnailScale(0.85f)
                .imageEngine(new GlideImageLoader.Glide4Engine())
                .showSingleMediaType(false)
                .forResult(requestCode);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == ImgCropUrtil.RESULT_CAMERA_CROP_PATH_RESULT) {
                String imageClip = ImgCropUrtil.getImageClip();
                checkFace(imageClip);
            } else if (requestCode == 1) {
                if (data != null) {
                    List<String> mSelected = Matisse.obtainPathResult(data);
                    if (mSelected != null && mSelected.size() > 0) {
                        //裁剪图片
                        ImgCropUrtil.crop(this, mSelected.get(0), 400, 400);
                    }
                }
            }
        }
    }

    private void checkFace(String facePath) {
        showProgress();
        Glide.with(this).load(facePath).into(userFace);
        File file = new File(facePath);
        HttpRequest.getService().faceDetection(HttpRequest.getPartFileBody(file)).enqueue(new Callback<BaseBean<FaceDetectionBean>>() {
            @Override
            public void onResponse(Call<BaseBean<FaceDetectionBean>> call, Response<BaseBean<FaceDetectionBean>> response) {
                hideProgress();
                BaseBean<FaceDetectionBean> body = response.body();
                boolean ok = false;
                if (body.getCode() == 200) {
                    FaceDetectionBean data = body.getData();
                    List<FaceDetectionBean.FaceListBean> face_list = data.getFace_list();
                    if (face_list != null && !face_list.isEmpty()) {
                        FaceDetectionBean.FaceListBean faceBean = face_list.get(0);
                        if (faceBean != null) {
                            if (faceBean.getFace_probability() == 1) {
                                ok = true;
                            }
                        }
                    }
                }
                startActivity(new Intent(UpdateFaceActivity.this, UpdateFaceResultActivity.class)
                        .putExtra("file", file)
                        .putExtra("ok", ok));
            }

            @Override
            public void onFailure(Call<BaseBean<FaceDetectionBean>> call, Throwable t) {
                hideProgress();
                ToastUtil.showTextToast(UpdateFaceActivity.this, getString(R.string.network_error));
            }
        });
    }
}
